import request from "@/utils/request";
import type { IUserListResData, IUserBase, IConmmonUserRes } from "./type";
import type { ICommonDataRes } from "@/types";

enum API {
  ALLUSER_URL = "/admin/acl/user/paginate",
  QUREYUSER_URL = "/admin/acl/user/query",
  USERBASE_URL = "/admin/acl/user",
  bulkDeleteUser_URL = "/admin/acl/user/bulkDelete",
}

export enum SelectKey {
  name = "name",
  username = "username",
}
/**
 * 根据页码及页面显示条数获取用户列表
 * @param page 页码
 * @param limit 页面显示条数
 * @returns 用户列表数组
 */
export const getAllUserApi = (
  page = 1,
  limit = 10,
  key = SelectKey.name,
  val = ""
) =>
  request.get<any, IUserListResData>(
    API.ALLUSER_URL + `?page=${page}&limit=${limit}&key=${key}&val=${val}`
  );

export const getUserByIdApi = (id: string) =>
  request.get<any, ICommonDataRes<IUserBase>>(API.USERBASE_URL + `/${id}`);

// export const getUserByUsernameApi = (username: string) =>
//   request.get<any, ICommonDataRes<IUserBase>>(API. + `?username=${username}`);

/**
 * 修改或增加用户API，根据参数是否包含_id判断
 * @param data
 * @returns
 */
export const addOrUpdateUserApi = (data: IUserBase) => {
  const { _id } = data;
  delete data.createdAt;
  delete data.updatedAt;
  if (_id) {
    delete data._id;
    delete data.userId;
    return request.put<any, IConmmonUserRes>(
      API.USERBASE_URL + `/${_id}`,
      data
    );
  } else {
    return request.post<any, IConmmonUserRes>(API.USERBASE_URL, data);
  }
};

/**
 * 删除用户API
 * @param id _id
 * @returns
 */
export const deleteUserApi = (id: string) =>
  request.delete<any, IConmmonUserRes>(API.USERBASE_URL + `/${id}`);

/**
 * 批量删除用户API
 * @param ids _id数组
 * @returns
 */
export const bulkDeleteUserApi = (ids: string[]) =>
  request.post<any, IConmmonUserRes>(API.bulkDeleteUser_URL, ids);
